### PACKAGE PREAMBLE ###

# Create a vector of package names
packages <- c("dplyr", "ggplot2", "tidyr", "tidyverse", "data.table", "readxl", "foreign", "devtools", "fixest", "sandwich", "modelsummary", "kableExtra")

installed_packages <- packages %in% rownames(installed.packages())
if (any(installed_packages == FALSE)) {
  install.packages(packages[!installed_packages])
}

# Packages loading
invisible(lapply(packages, library, character.only = TRUE))
rm(packages, installed_packages)

options(modelsummary_format_numeric_latex = "plain")
f <- function(x) format(x, digits = 3, nsmall = 2, scientific = FALSE)

getwd()
setwd(dir = "/Users/jmc4qg/The Lab Dropbox/Jonathan Colmer/ShotSpotter_env/Journal_submissions/ReStat/Replication Folder/")
mydata <- read.dta("Analysis Data/NIBRS_analysis.dta")

# Panel A

DiT_3 <- feols(homicide_pc~ temp_MP + prec_MP | sample_month + sample_month[tMean] +sample_month[MP] + sample_month[prec] + week + dow, data=mydata)
summary(DiT_3, cluster="STATE")
DiT_3_G <- feols(gun_homicide_pc~ temp_MP + prec_MP | sample_month + sample_month[tMean] +sample_month[MP] + sample_month[prec] + week + dow, data=mydata)
summary(DiT_3_G, cluster="STATE")
DiT_3_NG <- feols(ngun_homicide_pc~ temp_MP + prec_MP | sample_month + sample_month[tMean] +sample_month[MP] + sample_month[prec] + week + dow, data=mydata)
summary(DiT_3_NG, cluster="STATE")

DiDiT_6 <- feols(homicide_pc~temp_MP + prec_MP | ori_sample_month+ORI[tMean]+sample_month[tMean]+ORI[prec]+sample_month[prec] + week + dow, data=mydata)
summary(DiDiT_6, cluster="STATE")
DiDiT_6_G <- feols(gun_homicide_pc~temp_MP + prec_MP | ori_sample_month+ORI[tMean]+sample_month[tMean]+ORI[prec]+sample_month[prec] + week + dow, data=mydata)
summary(DiDiT_6_G, cluster="STATE")
DiDiT_6_NG <- feols(ngun_homicide_pc~temp_MP + prec_MP | ori_sample_month+ORI[tMean]+sample_month[tMean]+ORI[prec]+sample_month[prec] + week + dow, data=mydata)
summary(DiDiT_6_NG, cluster="STATE")

models <- list(DiT_3, DiT_3_G, DiT_3_NG, DiDiT_6, DiDiT_6_G, DiDiT_6_NG)
modelsummary(models, vcov = ~STATE, output = "Figures and Tables/Table_4/Table_4_Panel_A.tex", fmt = f, stars = c('*' = 0.1, '**' = 0.05, '***' = 0.01))

#Panel B

DiT_3_agg <- feols(agg_assault_pc~ temp_MP + prec_MP | sample_month + sample_month[tMean] +sample_month[MP] + sample_month[prec] + week + dow, data=mydata)
summary(DiT_3_agg, cluster="STATE")
DiT_3_agg_G <- feols(gun_agg_assault_pc~ temp_MP + prec_MP | sample_month + sample_month[tMean] +sample_month[MP] + sample_month[prec] + week + dow, data=mydata)
summary(DiT_3_agg_G, cluster="STATE")
DiT_3_agg_NG <- feols(ngun_agg_assault_pc~ temp_MP + prec_MP | sample_month + sample_month[tMean] +sample_month[MP] + sample_month[prec] + week + dow, data=mydata)
summary(DiT_3_agg_NG, cluster="STATE")

DiDiT_6_agg <- feols(agg_assault_pc~temp_MP + prec_MP | ori_sample_month+ORI[tMean]+sample_month[tMean]+ORI[prec]+sample_month[prec] + week + dow, data=mydata)
summary(DiDiT_6_agg, cluster="STATE")
DiDiT_6_agg_G <- feols(gun_agg_assault_pc~temp_MP + prec_MP | ori_sample_month+ORI[tMean]+sample_month[tMean]+ORI[prec]+sample_month[prec] + week + dow, data=mydata)
summary(DiDiT_6_agg_G, cluster="STATE")
DiDiT_6_agg_NG <- feols(ngun_agg_assault_pc~temp_MP + prec_MP | ori_sample_month+ORI[tMean]+sample_month[tMean]+ORI[prec]+sample_month[prec] + week + dow, data=mydata)
summary(DiDiT_6_agg_NG, cluster="STATE")

models <- list(DiT_3_agg, DiT_3_agg_G, DiT_3_agg_NG, DiDiT_6_agg, DiDiT_6_agg_G, DiDiT_6_agg_NG)
modelsummary(models, vcov = ~STATE,  output = "Figures and Tables/Table_4/Table_4_Panel_B.tex", fmt = f, stars = c('*' = 0.1, '**' = 0.05, '***' = 0.01))
